package com.gnight.ImgFace;/*
 *  @author: G_night
 *  转载请申明作者
 *  Reprint please state the author
 */

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.imageseg.model.v20191230.*;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;
import com.gnight.staticUtils.createOssUrl;
import java.io.IOException;

public class ALiYunImageseg {

    public static void main(String[] args) throws IOException {

        //前面的修改地域id和ram账号
        DefaultProfile profile = DefaultProfile.getProfile(
                "cn-shanghai",          // 地域ID
                "",// RAM账号的AccessKey ID
                "");

        // 写连接或者自己本地的文件，建议是本地文件免得超时
        String path="";
        IAcsClient client = new DefaultAcsClient(profile);
        SegmentFaceRequest request = new SegmentFaceRequest();
        request.setRegionId("cn-shanghai");
        request.setImageURL(createOssUrl.testUploadFile(path));
        try {
            SegmentFaceResponse response = client.getAcsResponse(request);
            String string=new Gson().toJson(response);
            JSONObject jsonObject= JSON.parseObject(string);
            JSONObject data = jsonObject.getJSONObject("data");
            JSONArray jsonArray=data.getJSONArray("elements");
            JSONObject object=jsonArray.getJSONObject(0);
            String url=object.getString("imageURL");
            System.out.println(url);
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            System.out.println("ErrCode:" + e.getErrCode());
            System.out.println("ErrMsg:" + e.getErrMsg());
            System.out.println("RequestId:" + e.getRequestId());
        }
    }
}
